package com.meixiang.activity.account.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbToastUtil;
import com.meixiang.util.DesUtil;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置登录密码
 *
 * @author wzz
 */
public class ReviseLoginPasswordActivity extends BaseActivity {
    @Bind(R.id.tv_show_phone)
    TextView mTvShowPhone;
    @Bind(R.id.title)
    TitleView mTitle;
    @Bind(R.id.cet_input_identifying_code)
    ClearEditText mCetInputIdentifyingCode;
    @Bind(R.id.cet_input_new_pwd)
    ClearEditText mCetInputNewPwd;
    @Bind(R.id.tv_confirm)
    CheckEditTextEmptyButton mTvConfirm;


    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_revise_login_pwd);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        mTitle.setTitle(R.string.change_password);
        mTitle.setLeftImageButton(R.mipmap.back);
        mTitle.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void initView() {
        if (!TextUtils.isEmpty(getIntent().getStringExtra("phone"))) {
            phone = getIntent().getStringExtra("phone");
            mTvShowPhone.setText("验证码已发送至号码" + phone);
        }
        mTvConfirm.setEditText(new EditText[]{mCetInputIdentifyingCode, mCetInputNewPwd});
    }

    @Override
    protected void setData() {

    }


    @Override
    @OnClick(R.id.tv_confirm)
    public void onClick(View v) {
        if (mCetInputNewPwd.getText().toString().trim().length() < 6) {
            AbToastUtil.showToast(context, "请输入新密码");
            return;
        }
        getChangePassword();
    }

    private void getChangePassword() {

        HttpParams params = new HttpParams();
        params.put("password", DesUtil.md5(mCetInputNewPwd.getText().toString().trim()));
        params.put("mobile", phone);
        params.put("mobileCode", mCetInputIdentifyingCode.getText().toString().trim());
        HttpUtils.post(Config.SERVER_URL + "checkValidCodePwd", params, new HttpCallBack(this, "正在修改密码...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (code.equals("0")) {
                    finish();
                    AbToastUtil.showToast(ReviseLoginPasswordActivity.this, message);
                } else {
                    mCetInputIdentifyingCode.setText("");
                    mCetInputNewPwd.setText("");
                    AbToastUtil.showToast(ReviseLoginPasswordActivity.this, message);
                }
            }

            @Override
            public void onError(String code, String message) {
                mCetInputIdentifyingCode.setText("");
                mCetInputNewPwd.setText("");
                AbToastUtil.showToast(ReviseLoginPasswordActivity.this, message);
            }
        });
    }

}
